-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-132631: Fix json.tool when using --json-lines and a file as input #138961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
This is perhaps the most minimal fix, however, there's an alternative version that would be more invasive. Some observations:
|
Lib/json/tool.py
Outdated
| for obj in objs: | ||
| json.dump(obj, outfile, **dump_args) | ||
| outfile.write('\n') | ||
| if infile is not sys.stdin: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a finally I think. If there is an exception happening in json.dumps for instance, we would have it left open. So this should be moved after the except ValueError as e with some finally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried looking at the git history of this file and it's a bit tricky to understand why it's not using content managers. At one point it did have context manager approach, then it was migrated to manually calling of open.
If there's a friction point, it's the "in place" feature. I don't think the in place feature/requirement ever fundamentally worked for the json lines case.
Lib/json/tool.py
Outdated
| if options.json_lines: | ||
| objs = (json.loads(line) for line in infile) | ||
| else: | ||
| objs = (json.load(infile),) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one can fail directl, so please be careful that we close the infile beforehand (that's the reaason why we have the try-finally in the first place).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're probably using a generator here to save memory. Otherwise we could've just used a list comprehension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment was about the else branch. It's not a generator, it's a regular tuple.
|
See PR #132632 for another attempt. |
|
The only test that is failing for me locally is the The "in place" functionality is a bit surprising and doesn't really have clear requirements around error handling. It's a bit disconcerting that in the "in place" mode you can overwrite/delete the original file if there's an error. |
Uh oh!
There was an error while loading. Please reload this page.